#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define stream(line) istringstream in((line))
#define endl "\n"
#define F first
#define S second
#define clr(a, d) memset((a),d,sizeof(a))
const int MOD = 1e9 + 7;
const int N = 1000 + 5;
const int M = 2000 + 5;
void print_vector(vector<ll> &v) {
for (ll i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << endl;
}
int head[N],to[M], nxt[M], cost[M], vis[N],n,m, mn, last, ne, indeg[N], have[N];
void addEdge(int f,int t, int c){
to[ne]=t;
cost[ne] = c;
nxt[ne]=head[f];
head[f]=ne++;
}
void init(){
memset(head,-1,sizeof(head));
ne=0;
}
void addBiEdge(int u,int v, int w){
addEdge(u,v,w);
}
void dfs(int u){
vis[u] = 1;
last = u;
for (int e = head[u],v,w; ~e&&(v=to[e],1) && (w = cost[e],1) ; e=nxt[e]) {
if(!vis[v]){
dfs(v);
if(w < mn){
mn = w;
}
}
}
}
int main() {
fast
int tc =1;
//cin >> tc;
while(tc--){
cin >> n >> m;
init();
for (int i = 0; i < m; ++i) {
int u,v,w;
cin >> u >> v >> w;
addBiEdge(u,v,w);
indeg[v]++;
have[u]++;
have[v]++;
}
vector<pair<pair<int,int>,int>> res;
for (int i = 1; i <= n; ++i) {
if(indeg[i] == 0 && have[i] != 0){
mn = 1e7;
dfs(i);
res.push_back({{i,last}, mn});
}
}
cout << res.size() << endl;
for(auto x : res){
cout << x.first.first << " " << x.first.second << " " << x.second << endl;
}
}
return 0;
}
394. Decode String | 902. Numbers At Most N Given Digit Set |
221. Maximal Square | 1200. Minimum Absolute Difference |
1619B - Squares and Cubes | 1619A - Square String |
1629B - GCD Arrays | 1629A - Download More RAM |
1629C - Meximum Array | 1629D - Peculiar Movie Preferences |
1629E - Grid Xor | 1629F1 - Game on Sum (Easy Version) |
2148. Count Elements With Strictly Smaller and Greater Elements | 2149. Rearrange Array Elements by Sign |
2150. Find All Lonely Numbers in the Array | 2151. Maximum Good People Based on Statements |
2144. Minimum Cost of Buying Candies With Discount | Non empty subsets |
1630A - And Matching | 1630B - Range and Partition |
1630C - Paint the Middle | 1630D - Flipping Range |
1328A - Divisibility Problem | 339A - Helpful Maths |
4A - Watermelon | 476A - Dreamoon and Stairs |
1409A - Yet Another Two Integers Problem | 977A - Wrong Subtraction |
263A - Beautiful Matrix | 180C - Letter |